function deepCopy(obj) {
  let help; // 辅助变量
  if (Object.prototype.toString.call(obj).slice(8, -1) === "Array") {
    // 数组
    help = [];
  } else {
    // 对象
    help = {};
  }

  (function _copy(obj, help, key) {
    if (key != null) {
      if (Object.prototype.toString.call(obj[key]).slice(8, -1) === "Object") {
        help[key] = {};
        for (var item in obj[key]) {
          if (obj[key].hasOwnProperty(item)) {
            _copy(obj[key], help[key], item);
          }
        }
      } else if (
        Object.prototype.toString.call(obj[key]).slice(8, -1) === "Array"
      ) {
        help[key] = [];
        obj[key].forEach((item, index) => {
          _copy(obj[key], help[key], index);
        });
      } else {
        help[key] = obj[key];
      }
    } else {
      if (Object.prototype.toString.call(obj).slice(8, -1) === "Array") {
        // 数组
        obj.forEach((item, index) => {
          _copy(obj, help, index);
        });
      } else {
        // 对象
        for (var key in obj) {
          if (obj.hasOwnProperty(key)) {
            _copy(obj, help, key);
          }
        }
      }
    }
  })(obj, help);
  return help;
}

let a = [1, 2, 3, 4];
let helpArray = deepCopy(a);
a[0] = 99;
console.log(helpArray);

let obj = {
  x: [
    1,
    [1, 2, 3],
    {
      x: 1,
      y: 2
    },
    4
  ],
  y: {
    z: 2,
    m: function() {}
  }
};

let helpObj = deepCopy(obj);
obj.x[2] = 99;
console.log(helpObj);

// let x = obj
// console.log(x); // 已经变为了99 ，浏览器的console.log机制？？？？？
// obj.x[2] = 99
// console.log(x);
